<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 文章标题
 *
 * admin-ArticleTitle
 *
 * @Apidoc\Title("文章标题")
 * @Apidoc\Group("api")
 */
class ArticleTitle extends BaseController
{
    /**
     * 新增
     *
     * admin-ArticleTitle-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\ArticleTitle::create($data['article_title']);
            return success();
        } else {
            $options = [];
            $article_title__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['article_title__member_id_options'] = $article_title__member_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-ArticleTitle-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\ArticleTitle::update($data['article_title']);
            return success();
        } else {
            $data = \app\common\model\ArticleTitle::find(input('article_title__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['article_title__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $article_title__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['article_title__member_id_options'] = $article_title__member_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-ArticleTitle-delete
     */
    public function delete()
    {
        \app\common\model\ArticleTitle::destroy(input('article_title__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-ArticleTitle-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_article_title');
        $query->join('db_member', 'db_member.id = db_article_title.member_id');
        if (input('member__id', '') !== '') {
            $query->where('db_member.id', '=', input('member__id', '', 'search_in'));
        }
        $query->field(['db_article_title.id' => 'article_title__id']);
        $query->field(['db_article_title.create_time' => 'article_create_time']);
        $query->field(['db_article_title.key_word' => 'article_title__key_word']);
        $query->field(['db_article_title.generated_quantity' => 'article_title__generated_quantity']);
        $query->field(['db_article_title.ip_address' => 'article_title__ip_address']);
        $query->where('db_article_title.key_word', 'like', '%'.input('article_title__key_word','').'%');

        $query->field(['db_member.name' => 'member__name']);
        $query->order('db_article_title.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        View::assign($options);
        return View::fetch();
    }
}
